package com.yanqu.road.server.cmd.activity.xingyoushanshui.cross;

import com.yanqu.road.entity.activity.youxing.XingYouShanShuiUserData;
import com.yanqu.road.entity.activity.youxing.config.XingYouConfig;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.XingYouShanShuiProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.shanshui.XingYouShanShuiModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.activity.shanshui.XingYouShanShuiMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

@Cmd(code = Protocol.S_XING_YOU_SHAN_SHUI_ENTER_MAP_FROM_CROSS,desc = "进入地图-跨服回包")
public class ShanShuiEnterMapFromCrossCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        XingYouShanShuiProto.CrossShanShuiEnterMapRsp crossRsp = XingYouShanShuiProto.CrossShanShuiEnterMapRsp.parseFrom(packet.getBytes());
        XingYouShanShuiProto.ShanShuiEnterMapRsp rsp = crossRsp.getRsp();
        //漂泊结算
        if(rsp.hasDriftResult()){
            String reward = rsp.getDriftResult().getReward();
            if(!StringUtils.isNullOrEmpty(reward)){
                Property property = PropertyHelper.parseStringToProperty(reward);
                XingYouConfig config = XingYouShanShuiMgr.getConfig();
                if(config != null){
                    property.removeProperty(config.getSHANSHUI_SILVER_COIN_ID());
                }
                player.getModule(CurrencyModule.class).addCurrency(property, eLogMoneyType.XingYouShanShuiActivity, eLogMoneyType.XingYouShanShuiDriftBackReward);
            }
        }
        //特殊处理下，需要同步个人积分（非玩家请求）
        if(rsp.getRet() != -10) {
            player.sendPacket(Protocol.U_XING_YOU_SHAN_SHUI_ENTER_MAP, rsp.toBuilder());
        }

        if(crossRsp.hasSilverCoinTotal()){
            if(player == null){
                player = GamePlayerMgr.getPlayer(packet.getPlayerId());
            }
            XingYouShanShuiUserData userData = player.getModule(XingYouShanShuiModule.class).getUserData();
            if (userData != null) {
                userData.setTotalSilverCoin(crossRsp.getSilverCoinTotal());
                player.notifyListener(eGamePlayerEventType.XingYouShanShuiUserSliverRank.getValue(), userData.getTotalSilverCoin());
            }
        }
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
